#ifndef __CCFPROJECTFILEPARSER_H_
#define __CCFPROJECTFILEPARSER_H_

#include <QObject>
#include <jni.h>
#include <pthread.h>

#ifdef Q_OS_WIN
#include <QAxFactory>
#define CCF_MAIN_BEGIN(LibName, Package, axLibId, axAppId)  QAXFACTORY_BEGIN(axLibId, axAppId)
#define CCF_CLASS(Class, Header, Sources)  QAXCLASS(Class)
#define CCF_MAIN_END()  QAXFACTORY_END()
#else
#define CCF_MAIN_BEGIN(LibName, Package, axLibId, axAppId)
#define CCF_CLASS(Class, Header, Sources)
#define CCF_MAIN_END()
#endif

extern "C"
{
// ------ 项目文件编辑器视图 ------
JNIEXPORT jlong JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_createControl(JNIEnv *, jclass, jlong, jlong);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_computeSize(JNIEnv *, jclass, jlong, jintArray);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_resizeControl(JNIEnv *, jclass, jlong, jint, jint, jint, jint);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_disposeControl(JNIEnv *, jclass, jlong);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_setFont(JNIEnv *, jclass, jlong, jstring, jint);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_showModel(JNIEnv *, jclass, jlong, jstring);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_reload(JNIEnv *, jclass, jlong);
JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_save(JNIEnv *, jclass, jlong);
JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_isDirty(JNIEnv *, jclass, jlong);
JNIEXPORT jstring JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_contents(JNIEnv *, jclass, jlong);
JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_isActionEnabled(JNIEnv *, jclass, jlong, jint);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ProjectFileEditorView_triggerAction(JNIEnv *, jclass, jlong, jint);

// ------ ScopeList ------
JNIEXPORT jlong JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_createControl(JNIEnv *, jclass, jlong, jlong);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_computeSize(JNIEnv *, jclass, jlong, jintArray);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_resizeControl(JNIEnv *, jclass, jlong, jint, jint, jint, jint);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_disposeControl(JNIEnv *, jclass, jlong);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_setFont(JNIEnv *, jclass, jlong, jstring, jint);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_showModel(JNIEnv *, jclass, jlong, jstring, jstring, jboolean);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_selectFirstVariable(JNIEnv *, jclass, jlong);
JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_search(JNIEnv *, jclass, jlong, jstring, jstring);
JNIEXPORT jboolean JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_isChanged(JNIEnv *, jclass, jlong, jstring);
JNIEXPORT jstring JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_removeFiles(JNIEnv *, jclass, jlong, jstring);
JNIEXPORT jstring JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_addFiles(JNIEnv *, jclass, jlong, jstring);
JNIEXPORT void JNICALL Java_ide_ccf_ccproject_pages_embedded_ScopeList_addFile(JNIEnv *, jclass, jlong, jstring, jstring);
}

class ProjectFileEditorViewListener : public QObject
{
    Q_OBJECT
public:
    ProjectFileEditorViewListener(QObject *parent, pthread_key_t *key, jclass that) : QObject(parent)
    {
        envKey = key;
        JNIEnv *env = (JNIEnv *)pthread_getspecific(*envKey);
        javaClass = (jclass) env->NewGlobalRef((jobject)that);
        jmId[0] = env->GetStaticMethodID(that, "actionChanged", "(JI)V");
        jmId[1] = env->GetStaticMethodID(that, "changed", "(J)V");
    }

    ~ProjectFileEditorViewListener() { }

public Q_SLOTS:
    void slot_actionChanged(int id)
    {
        JNIEnv *env = (JNIEnv *)pthread_getspecific(*envKey);
        if (env == NULL) return;
        env->CallStaticVoidMethod(javaClass, jmId[0], (jlong)parent(), id);
    }

    void slot_changed()
    {
        JNIEnv *env = (JNIEnv *)pthread_getspecific(*envKey);
        if (env == NULL) return;
        env->CallStaticVoidMethod(javaClass, jmId[1], (jlong)parent());
    }

private:
    pthread_key_t *envKey;
    jclass javaClass;
    jmethodID jmId[2];
};

#endif //__CCFPROJECTFILEPARSER_H_
